using System;
using System.Collections.Generic;
using System.Text;

namespace BlackjackGUI
{
   class CBasicStrategy
   {
      public enum MoveEnum
      {
         Hit,
         Stand,
         Double,
         Split
      }

      public MoveEnum MakeMove(ValueEnum dealerCard, CHand playerHand)
      {
         MoveEnum retval = MoveEnum.Stand;
         if (playerHand.Splitable)
         {
            // use doubles table
            retval = CheckDoublesTable(dealerCard, playerHand);
         }
         else if (playerHand.SoftHand)
         {
            // use soft hand table
            retval = CheckSoftTable(dealerCard, playerHand);
         }
         else
         {
            // use regular table
            retval = CheckRegularTable(dealerCard, playerHand);
         }
         return retval;
      }

      private MoveEnum CheckSoftTable(ValueEnum dealerCard, CHand playerHand)
      {
         switch (playerHand.Total)
         {
            case 21:
            case 20:
               return MoveEnum.Stand;
            case 19:
               switch (dealerCard)
               {
                  case ValueEnum.SIX:
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Stand;
                  default:
                     return MoveEnum.Stand;
               }
            case 18:
               switch (dealerCard)
               {
                  case ValueEnum.TWO:
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Stand;
                  case ValueEnum.SEVEN:
                  case ValueEnum.EIGHT:
                     return MoveEnum.Stand;
                  default:
                     return MoveEnum.Hit;
               }
            case 17:
               switch (dealerCard)
               {
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Hit;
                  default:
                     return MoveEnum.Hit;
               }
            case 16:
            case 15:
               switch (dealerCard)
               {
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Hit;
                  default:
                     return MoveEnum.Hit;
               }
            case 14:
            case 13:
               switch (dealerCard)
               {
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Hit;
                  default:
                     return MoveEnum.Hit;
               }

            default:
               throw new Exception("CheckSoftTable Basic Strategy Error\nContact Pete please!");
         }
      }

      private MoveEnum CheckDoublesTable(ValueEnum dealerCard, CHand playerHand)
      {
         switch (playerHand[0].GetValue())
         {
            case ValueEnum.ACE:
            case ValueEnum.EIGHT:
               return MoveEnum.Split;
            case ValueEnum.TEN:
            case ValueEnum.JACK:
            case ValueEnum.QUEEN:
            case ValueEnum.KING:
               return MoveEnum.Stand;
            case ValueEnum.NINE:
               switch (dealerCard)
               {
                  case ValueEnum.SEVEN:
                  case ValueEnum.TEN:
                  case ValueEnum.ACE:
                     return MoveEnum.Stand;
                  default:
                     return MoveEnum.Split;
               }
            case ValueEnum.SEVEN:
               switch (dealerCard)
               {
                  case ValueEnum.EIGHT:
                  case ValueEnum.NINE:
                  case ValueEnum.TEN:
                  case ValueEnum.ACE:
                     return MoveEnum.Hit;
                  default:
                     return MoveEnum.Split;
               }
            case ValueEnum.SIX:
               switch (dealerCard)
               {
                  case ValueEnum.TWO:
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     return MoveEnum.Split;
                  default:
                     return MoveEnum.Hit;
               }
            case ValueEnum.FIVE:
               switch (dealerCard)
               {
                  case ValueEnum.TEN:
                  case ValueEnum.ACE:
                     return MoveEnum.Hit;
                  default:
                     return MoveEnum.Double;
               }
            case ValueEnum.FOUR:
               switch (dealerCard)
               {
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     return MoveEnum.Split;
                  default:
                     return MoveEnum.Hit;
               }
            case ValueEnum.THREE:
            case ValueEnum.TWO:
               switch (dealerCard)
               {
                  case ValueEnum.TWO:
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                  case ValueEnum.SEVEN:
                     return MoveEnum.Split;
                  default:
                     return MoveEnum.Hit;
               }
            default:
               throw new Exception("Error in CheckDoublesTable of Basic Strategy\nPlease Contact Pete!");
         }
      }
      private MoveEnum CheckRegularTable(ValueEnum dealerCard, CHand playerHand)
      {
         switch (playerHand.Total)
         {
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
               return MoveEnum.Stand;
            case 16:
            case 15:
            case 14:
            case 13:
               switch(dealerCard)
               {
                  case ValueEnum.TWO:
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     return MoveEnum.Stand;
                  default :
                     return MoveEnum.Hit;
               }
            case 12:
               switch(dealerCard)
               {
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     return MoveEnum.Stand;
                  default :
                     return MoveEnum.Hit;
               }
            case 11:
               if( playerHand.Count == 2 )
               {
                  return MoveEnum.Double;
               }
               else
               {
                  return MoveEnum.Hit;
               }
            case 10:
               switch(dealerCard)
               {
                  case ValueEnum.TEN:
                  case ValueEnum.ACE:
                     return MoveEnum.Hit;
                  default :
                     if (playerHand.Count == 2)
                        return MoveEnum.Double;
                     else
                        return MoveEnum.Hit;
               }
            case 9:
               switch (dealerCard)
               {
                  case ValueEnum.THREE:
                  case ValueEnum.FOUR:
                  case ValueEnum.FIVE:
                  case ValueEnum.SIX:
                     return MoveEnum.Double;
                  default :
                     return MoveEnum.Hit;
               }
            case 8:
            case 7:
            case 6:
            case 5:
               return MoveEnum.Hit;
            default :
               throw new Exception("Error in CheckRegularTable of Basic Strategy\nPlease contact Pete!");
         }
      }
   }
}
